home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1998 June / Cd Pc Users 9.iso / prog / inst / baslibs / basmappi.bas < prev    next >
Encoding:
BASIC Source File  |  1996-12-11  |  6.1 KB  |  278 lines

  1. Attribute VB_Name = "basMapping"
  2. Option Explicit
  3. '
  4. '  Routines using the clsMapping class.
  5. '
  6. '  Saves/loades 1 or more mappings from/to
  7. '  a file:
  8.  
  9. '  SaveMapping  - Saves a mapping to a file.
  10. '  LoadMapping  - Loads a mapping from a file.
  11. '
  12. '
  13. '  The following expect an open file handle:
  14. '
  15. '  ReadMapping  - Reads a mapping from a file.
  16. '  WriteMapping - Writes a mapping to a file.
  17. '
  18. '
  19. '  Saves or loads a mapping from the registry.
  20. '
  21. '  SaveSettingMapping
  22. '  LoadSettingMapping
  23. '
  24. '  There are some limitations on the types of data
  25. '  that can be saved and loaded.
  26. '    - Variant types vbObject, vbDataObject, and vbError
  27. '      can not be saved.
  28. '
  29.  
  30. Private Const DEFAULT_FILE_KEY = 1347436877  'In asci this is "MAPP"
  31. Private Const BASE_ERROR = 1000
  32.  
  33. Private vNull As Variant
  34. Public Sub SaveSettingsMapping(m As clsMapping, appname As Variant, Optional Key As Variant, Optional clearsetting As Variant)
  35.    Dim i As Integer
  36.    Dim AppN As String
  37.    Dim k As String
  38.    
  39.    If IsMissing(appname) Then
  40.       AppN = App.ProductName
  41.    Else
  42.       AppN = CStr(appname)
  43.    End If
  44.    
  45.    If IsMissing(Key) Then
  46.       k = "Settings"
  47.    Else
  48.       k = CStr(Key)
  49.    End If
  50.  
  51.    If Not IsMissing(clearsetting) Then
  52.       If clearsetting Then
  53.          On Error Resume Next
  54.          DeleteSetting AppN, k
  55.       End If
  56.    End If
  57.    
  58.    For i = 1 To m.Count
  59.       SaveSetting AppN, k, m.Key(i), m.Item(i)
  60.    Next i
  61. End Sub
  62.  
  63. Public Sub LoadSettingsMapping(m As clsMapping, appname As Variant, Optional Key As Variant)
  64.    Dim AppN As String
  65.    Dim k As String
  66.    Dim v As Variant
  67.    Dim i As Integer
  68.    
  69.    If IsMissing(appname) Then
  70.       AppN = App.ProductName
  71.    Else
  72.       AppN = CStr(appname)
  73.    End If
  74.    
  75.    If IsMissing(Key) Then
  76.       k = "Settings"
  77.    Else
  78.       k = CStr(Key)
  79.    End If
  80.    
  81.    v = GetAllSettings(AppN, k)
  82.    If Not IsEmpty(v) Then
  83.       For i = LBound(v, 1) To UBound(v, 1)
  84.          m.Item(v(i, 0)) = v(i, 1)
  85.       Next i
  86.    End If
  87. End Sub
  88.  
  89.  
  90. '
  91. '  If keynum = 0 then use the default keynum
  92. '  If Keynum = -1 then ignore any keynum
  93. '
  94. Public Sub LoadMapping(filename As String, KeyNum As Long, ParamArray m() As Variant)
  95.    Dim iErr As Integer
  96.    Dim sErr As String
  97.    Dim fh As Integer   ' File Handle
  98.    Dim l As Long
  99.    Dim i As Long
  100.    
  101.    On Error GoTo ErrorHandler
  102.    
  103.    fh = FreeFile(0)
  104.    Open filename For Binary Access Read Lock Read Write As fh
  105.    
  106.    Get fh, , i
  107.    
  108.    If KeyNum = 0 Then
  109.       l = CLng(DEFAULT_FILE_KEY)
  110.       If l <> i Then
  111.          Close fh
  112.          
  113.          On Error GoTo 0
  114.          Err.Raise BASE_ERROR + 1, "LoadMapping", "File is corrupt or of an unknown format."
  115.          
  116.          Exit Sub
  117.       End If
  118.    ElseIf KeyNum = -1 Then
  119.       ' do nothing
  120.    Else
  121.       l = CLng(KeyNum)
  122.       If l <> 0 Then
  123.          If l <> i Then
  124.             Close fh
  125.             
  126.             On Error GoTo 0
  127.             Err.Raise BASE_ERROR + 1, "LoadMapping", "File is corrupt or of an unknown format."
  128.             
  129.             Exit Sub
  130.          End If
  131.       End If
  132.    End If
  133.  
  134.    For i = 0 To UBound(m)
  135.       ReadMapping fh, m(i)
  136.    Next i
  137.    
  138.    Close fh
  139.    Exit Sub
  140.    
  141.    ' Do error handleing to make sure the file is
  142.    ' closed, then pass the error to the main
  143.    ' program
  144. ErrorHandler:
  145.    iErr = Err
  146.    sErr = Err.Description
  147.    
  148.    On Error Resume Next
  149.    Close fh
  150.    
  151.    On Error GoTo 0
  152.    Err.Raise iErr, "SaveMapping", sErr
  153. End Sub
  154. Public Sub ReadMapping(FileHandle As Integer, m As Variant)
  155.    Dim l As Long
  156.    Dim k As Variant
  157.    Dim v As Variant
  158.    Dim i As Long
  159.    
  160.    Get FileHandle, , l
  161.       
  162.    For i = 1 To l
  163.       Get FileHandle, , k
  164.       Get FileHandle, , v
  165.       
  166.       If Not IsNull(k) Then
  167.          m.Item(k) = v
  168.       End If
  169.    Next
  170. End Sub
  171.  
  172.  
  173. '
  174. '  Uses the default KeyNum if KeyNum = 0
  175. '
  176. Public Sub SaveMapping(filename As String, ByVal KeyNum As Long, ParamArray m() As Variant)
  177.    Dim iErr As Integer
  178.    Dim sErr As String
  179.    Dim fh As Integer   ' File Handle
  180.    Dim l As Long
  181.    Dim e As Boolean
  182.    Dim e2 As Boolean
  183.    Dim i As Long
  184.    
  185.    On Error GoTo ErrorHandler
  186.    
  187.    e2 = False
  188.    fh = FreeFile(0)
  189.    
  190.    On Error Resume Next
  191.    Kill filename
  192.    
  193.    On Error GoTo ErrorHandler
  194.    Open filename For Binary Access Write Lock Read Write As fh
  195.    
  196.    If KeyNum = 0 Then
  197.       l = CLng(DEFAULT_FILE_KEY)
  198.       Put fh, , l
  199.    Else
  200.       l = CLng(KeyNum)
  201.       Put fh, , l
  202.    End If
  203.    
  204.    For i = 0 To UBound(m)
  205.       e = WriteMapping(fh, m(i))
  206.       If Not e Then
  207.          e2 = True
  208.       End If
  209.    Next i
  210.    
  211.    Close fh
  212.    
  213.    On Error GoTo 0
  214.  
  215.    If e2 Then Err.Raise BASE_ERROR, "SaveMapping", _
  216.       "All data was not of a valid type.  Some data may not have been saved."
  217.    
  218.    Exit Sub
  219.    
  220.    
  221.    ' Do error handleing to make sure the file is
  222.    ' closed, then pass the error to the main
  223.    ' program
  224. ErrorHandler:
  225.    iErr = Err
  226.    sErr = Err.Description
  227.    
  228.    On Error Resume Next
  229.    Close fh
  230.    
  231.    On Error GoTo 0
  232.    Err.Raise iErr, "SaveMapping", sErr
  233. End Sub
  234.  
  235. '
  236. '  Returns 1 if the item was not of a valid type,
  237. '  and 0 if it was.
  238. '
  239. Private Function WriteItem(ByVal FileHandle As Integer, v As Variant) As Long
  240.    Select Case VarType(v) And Not vbArray
  241.       Case vbError, vbDataObject, vbObject:
  242.          Put FileHandle, , vNull
  243.          WriteItem = 1
  244.          
  245.       Case Else:
  246.          Put FileHandle, , v
  247.          WriteItem = 0
  248.    End Select
  249. End Function
  250. '
  251. '  Writes a mapping to the file associated with the
  252. '  handle 'FileHandle'.
  253. '
  254. '  Returns False if some data was not written because
  255. '  it was not of a type that could be saved.
  256. '
  257. Public Function WriteMapping(ByVal FileHandle As Integer, m As Variant) As Boolean
  258.    Dim l As Long
  259.    Dim i As Long
  260.    Dim k As Variant
  261.    Dim e As Long
  262.    
  263.    e = 0
  264.    l = m.Count
  265.    vNull = Null
  266.    
  267.    Put FileHandle, , l
  268.    
  269.    For i = 1 To l
  270.       k = m.Key(i)
  271.       
  272.       e = e + WriteItem(FileHandle, k)
  273.       e = e + WriteItem(FileHandle, m.Item(k))
  274.    Next i
  275.    
  276.    WriteMapping = IIf(e > 0, False, True)
  277. End Function
  278.